package com.yonyou.pmclouds.safeinspection.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.DocCenterProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.MobileNoticeInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.safeinspection.entity.SafeInspectionVO;
import com.yonyou.pmclouds.safeinspection.mapper.SafeinspectionMapper;
import com.yonyou.pmclouds.safeinspection.process.*;
import com.yonyou.pmclouds.safeinspection.rmiitf.SafeinspectionMaintainService;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;

/**
 * @author 徐老板
 * @date 2020/4/21   14:03
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass = SafeinspectionMaintainService.class)
public class SafeinspectionMaintainServiceImpl implements SafeinspectionMaintainService , IFlowApproveBuziService {

//    , IFlowApproveBuziService

    @Resource
    private SafeinspectionMapper maintainmapper;
    @Resource
    private ProjectstateBeforeProcess projectstateBeforeProcess;
    //计算推送数量
    @Resource
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<SafeInspectionVO> countprocess;
    //修改bill状态用的不是上面那个方法 用的是下面那个方法
    @Resource
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<SafeInspectionVO> updatebilltypeprocess;

    //调用示例图片方法
    @Resource
    private PictureRemoteService pictureRemoteService;
    @Resource
    private UpdateInspectItemProcess updateInspectItemProcess;
    //工程单据集成文档中心|审批通过集成到NCC中
    @Resource
    private DocCenterProcessor docCenterProcessor;
    @Resource
    private InspectitemaddAfterProcess inspectitemaddAfterProcess;
    //计算通过率
    @Resource
    private CountRateProcess countRateProcess;
    //抄送人发送通知
    @Resource
    private MobileNoticeInsertProcessor<SafeInspectionVO> mobileNoticeInsertProcessor;






    @Override
    public SafeInspectionVO add(SafeInspectionVO safeInspectionVO) throws BusinessException {

        if (safeInspectionVO==null){
            throw new BusinessException("添加操作传入实体类为空");
        }

        SingleInsertBpTemplate<SafeInspectionVO> insertBpTemplate = new SingleInsertBpTemplate<SafeInspectionVO>(maintainmapper);
        //非空项校验
        insertBpTemplate.addBeforeProcessor(new InsertchecknullBeforeProcess());
        //添加创建者 创建时间
        insertBpTemplate.addBeforeProcessor(new AuditInfoInsertProcessor<>());
        //对项目状态的校验
        insertBpTemplate.addBeforeProcessor(projectstateBeforeProcess);
        //计算通过率
        insertBpTemplate.addBeforeProcessor(countRateProcess);

        //这里还要把推送数量算一下 供项目模块显示
        insertBpTemplate.addAfterProcessor(countprocess.getProcessor(BillTypeConst.SAFEINSPECTION_BILLTYPE));
        //给检查记录表添加内容
        insertBpTemplate.addAfterProcessor(inspectitemaddAfterProcess);

        insertBpTemplate.insert(safeInspectionVO);
        //添加示例图片
        pictureRemoteService.insertPictureForPub(safeInspectionVO, null);

        return safeInspectionVO;
    }

    @Override
    public SafeInspectionVO update(SafeInspectionVO safeInspectionVO) throws BusinessException {
        SingleUpdateBpTemplate<SafeInspectionVO> updateBpTemplate = new SingleUpdateBpTemplate<>(maintainmapper, new String[]{"checkArea", "billstatus"});
        //添加修改者 修改时间
        updateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
//        //对修改项进行非空校验
        updateBpTemplate.addBeforeProcessor(new InsertchecknullBeforeProcess());
//        //检查项目状态是否结束
        updateBpTemplate.addBeforeProcessor(projectstateBeforeProcess);
//        //添加这条数据进库 供数量显示 这个待测试 单元测试的时候一直报重复主键 这个联调的时候测试一下 不行的话就换方法
        updateBpTemplate.addAfterProcessor(updatebilltypeprocess.getProcessor(BillTypeConst.SAFEINSPECTION_BILLTYPE));
//        //修改检查记录
        updateBpTemplate.addAfterProcessor(updateInspectItemProcess);

        updateBpTemplate.update(safeInspectionVO);
        //这个最好查一下
        return safeInspectionVO;
    }

    @Override
    public SafeInspectionVO approve(String pk_safeinspection, byte billStatus) throws BusinessException {
        if (StringUtils.isEmpty(pk_safeinspection)){
            return null;
        }
        SafeInspectionVO byId = maintainmapper.getById(pk_safeinspection);
        //通过就是更新单据状态 这边只传来单据状态 单据类型用工具类设置
        SingleUpdateBpTemplate<SafeInspectionVO> updateBpTemplate =new SingleUpdateBpTemplate<>(maintainmapper,new String[]{"bill_type"});
        byId.setBillstatus(billStatus);
        //相关设置已经在update方法中执行 这边只修改单据相关数据
        updateBpTemplate.addAfterProcessor(countprocess.getProcessor(BillTypeConst.SAFEINSPECTION_BILLTYPE));
        //这边和驳回的区别是审批成功还要推送数据到[工程单据集成文档中心]
        updateBpTemplate.addAfterProcessor(docCenterProcessor);
        updateBpTemplate.update(byId);

        return maintainmapper.getById(pk_safeinspection);
    }

    //通过不通过这边就是修改单据状态和单据类型
    @Override
    public SafeInspectionVO reject(String pk_safeinspection) throws BusinessException {
        if (StringUtils.isEmpty(pk_safeinspection)){
            return null;
        }
        SafeInspectionVO byId = maintainmapper.getById(pk_safeinspection);
        SingleUpdateBpTemplate<SafeInspectionVO> updateBpTemplate =new SingleUpdateBpTemplate<>(maintainmapper,new String[]{"bill_type"});
        byId.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        //修改单据类型用process工具类
        //被拒绝这边虽然不会推送到NCC集成单据部分 但是会生成数量
        updateBpTemplate.addAfterProcessor(countprocess.getProcessor(BillTypeConst.SAFEINSPECTION_BILLTYPE));
        updateBpTemplate.update(byId);
        return maintainmapper.getById(pk_safeinspection);
    }


    //审批流需要实现的方法

    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        SafeInspectionVO byId = maintainmapper.getById(pk_bill);
        //这边单据状态审批通过以后有审批中和审批通过这个审批通过的方法里不需要设置这个单据状态
        SingleUpdateBpTemplate<SafeInspectionVO> updateBpTemplate = new SingleUpdateBpTemplate<>(maintainmapper, new String[]{"billstatus"});

        //给抄送人发生通知
        updateBpTemplate.addAfterProcessor(mobileNoticeInsertProcessor);
        //单据审批成功以后都会进入到NCC汇总中
        updateBpTemplate.addAfterProcessor(docCenterProcessor);
        updateBpTemplate.update(byId);

        return byId;
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        SafeInspectionVO vo = maintainmapper.getById(pk_bill);
        vo.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        SingleUpdateBpTemplate<SafeInspectionVO> updateBpTemplate = new SingleUpdateBpTemplate<>(maintainmapper,new String[]{"billstatus"} );
        updateBpTemplate.addAfterProcessor(updatebilltypeprocess.getProcessor(BillTypeConst.SAFEINSPECTION_BILLTYPE));
        updateBpTemplate.update(vo);
        return vo;
    }

    //这个直接用basemapper提供的现成方法
    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        return maintainmapper.getById(pk_bill);
    }

    @Override
    public String getBillType() {
        return BillTypeConst.SAFEINSPECTION_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<SafeInspectionVO> deleteBpTemplate = new SingleDeleteBpTemplate(maintainmapper);
        //删除要更新单据状态
        deleteBpTemplate.addAfterProcessor(updatebilltypeprocess.getProcessor(BillTypeConst.SAFEINSPECTION_BILLTYPE));
        SafeInspectionVO delete = deleteBpTemplate.delete((SafeInspectionVO) businessVO);
        return delete;
    }

    @Override
    public Object getMapper() {

        return maintainmapper;
    }


}
